﻿using MVVMLightDemo.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace MVVMLightDemo.Services
{
    public class SerializeService
    {
        static string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\Dishes.xml");
        public static void Serialize(List<Dish> listDishes)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Dish>));
                serializer.Serialize(fs, listDishes);
            }
        }


        public static List<Dish> Deserialize()
        {
            if (!File.Exists(path))
            {
                return null;
            }
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Dish>));
                List<Dish> listDish = serializer.Deserialize(fs) as List<Dish>;
                return listDish;
            }

        }
    }
}
